package com.ramblingcoder.bc2.server.tool.update.commands;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;

public class SupportingFunctions {
	public static String hashPassword(String hash, String password) throws NoSuchAlgorithmException {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		
		md5.update(hexToBytes(hash));
		md5.update(password.getBytes());
		
		byte[] thedigest = md5.digest();
		
		return getHex(thedigest);
	}
	
	public static byte[] hexToBytes(String hash) {
	    int len = hash.length();
	    
	    byte[] data = new byte[len / 2];
	    
	    for (int i = 0; i < len; i += 2)
	        data[i / 2] = (byte) ((Character.digit(hash.charAt(i), 16) << 4) + Character.digit(hash.charAt(i+1), 16));
	    
	    return data;
	}

	public static String getHex(byte[] raw) {
		String hexRange = "0123456789ABCDEF";
		
		if (raw == null) 
			return null;
		
		final StringBuilder hex = new StringBuilder(2 * raw.length);
		for (final byte b : raw)
			hex.append(hexRange.charAt((b & 0xF0) >> 4)).append(hexRange.charAt((b & 0x0F)));
		
		return hex.toString();
	}

	public static void sendCommand(OutputStream os, String[] strings) throws IOException {
		os.write(Command.packageParcel(strings));
	}
	
	public static Parcel[] getResponse(InputStream is) throws IOException {
		return getResponse(is, false);
	}
	public static Parcel[] getResponse(InputStream is, boolean longResponse) throws IOException {
		byte[] input = new byte[1024*32];
		int read = 0;
		 
		int previousLength = 0;
		while (longResponse){
			int nBytesAvail = is.available();
			
            if (nBytesAvail == 0 || nBytesAvail != previousLength)
				try {Thread.sleep(250);} catch (InterruptedException e) {}
			else if (previousLength == nBytesAvail){
				break;
			}
            
            previousLength = nBytesAvail;
		}
		
		read = is.read(input);
		
		return Response.unpackParcel(input, read);
	}

	public static void showError(final Context context, String message) {
		showError(context, message, null);
	}
	public static void showError(final Context context, String message, final Intent intent) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("An Error Occured");
		builder.setMessage(message);
		
		if (intent != null)
			builder.setPositiveButton("Close", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					((Activity)context).finish();
					context.startActivity(intent);
				}
			});
		else
			builder.setPositiveButton("Close", null);
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void showMessage(final Context context, String title, String string) {
		showError(context, string, null);
	}
	public static void showMessage(final Context context, String title, String string, final Intent intent) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(string);
		
		if (intent != null)
			builder.setPositiveButton("Close", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					((Activity)context).finish();
					context.startActivity(intent);
				}
			});
		else
			builder.setPositiveButton("Close", null);
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
}
